<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";

?>
<p class="small" align="left">
Campaigns > Add New
</p>
<p class="heading">
Add A New Campaign
</p>
<p> Fill out this simple form to add a new campaign and AdTrackzGold will create a 
  campaign for you and supply you with your tracking url. Place this url in your 
  ads and you will be able to track the source of your clicks and sales. <a href="http://www.adtrackzgold.com/manual" target=_new>Click 
  here</a> to learn how to use subcampaigns. </p>
<p> <a href="http://www.adtrackzgold.com/manual" target=_new>Click here</a> to 
  learn how to track actions and sales for any affiliate program you may be promoting. 
</p>
<br>
<?

if ($submit)
{
	$startdate = $fyear."-".$fmonth."-".$fday." 00:00:00";
	$enddate = $tyear."-".$tmonth."-".$tday." 00:00:00";

	//check variables
	if (strlen($adcode) > 100)
		error("adcode must be less than 100 characters.");
	elseif (!$adcode)
		error("ad code is a required field.");
	elseif (preg_match("/(\W+|\s+)/", $adcode, $matches))
		error("Ad code must contain letters,digits or and underscore character only.");
	elseif (strlen($url) > 255)
		error("Url must be less than 255 characters.");
	elseif (!$url)
		error("Url is a required field.");
	elseif (!preg_match("/(http|https):\/\//", $url))
		error("Url is not valid.");
	elseif (!preg_match("/\b\d+\b/", $cost))
		error("Cost must be in numerical format.");
	elseif (earlier_date($startdate,$enddate) == $enddate)
		error("Start date must be earlier than end date.");
	elseif ($recur && $ppc)
		error("Please select recurring or ppc. Both can't be selected at the same time.");
	elseif ($recur && (!$recurdays || !$repeat))
		error("Recurring cost and repeating cost must be filled out if recurring is selected.");
	elseif ($recurdays && !preg_match("/\b\d+\b/", $recurdays))
		error("Recurring days must be an integer value.");
	elseif ($repeat && !preg_match("/\b\d+\b/", $repeat))
		error("Repeat must be an integer value.");
	elseif ($recur && $cost == "0.00")
		error("You must enter a cost if you select recurring.");
	elseif ($ppc && $cost == "0.00")
		error("You must enter a cost if you select ppc.");
	elseif (mysql_num_rows(mysql_query("select * from adtrackz_campaigns where adcode='$adcode'")))
		error("That adcode has already been taken. Please choose another.");
	elseif ($merchant && $purpose != "affiliate")
		error("If you entered something in the merchant field, you must select 'Affiliate Link' as the purpose of your campaign.");
	else
	{
		if ($ppc)
			$ppc = 1;
		
		if ($group == "default")
			$groupID = "0";
		else
			$groupID = mysql_fetch_row(mysql_query("select ID from adtrackz_groups where ID='$group'"));

		$adcode = trim($adcode);
		$adcode = strtolower($adcode);		

		mysql_query("insert into adtrackz_campaigns values('','$groupID[0]','$adcode','$url','$cost','$startdate','$enddate','$description','$purpose','$recurdays','$repeat','$ppc')");

		//add split test option
		$newcampaignID = mysql_fetch_row(mysql_query("select ID from adtrackz_campaigns where adcode='$adcode'"));
		mysql_query("insert into adtrackz_campaigns_st values ('$newcampaignID[0]', '$st')");

		//mysql_query("insert into campaigns_optional values ('','$newcampaignID[0]','$optcontact','$optphone','$optemail','$opturl','$optnotes')");

		alert ("$adcode has been added successfully!<br><br>Select below for your new tracking url:<br> <textarea cols=50 rows=2>http://$domain/$udirectory/go.php?c=$adcode</textarea><br><br>Click this link to test it: <a href=http://$domain/$udirectory/go.php?c=$adcode target=_new>http://$domain/$udirectory/go.php?c=$adcode</a>.");

		unset($adcode, $group, $purpose, $url, $cost, $ppc, $recurdays, $repeat, $fmonth, $fday, $fyear, $tmonth, $tday, $tyear, $description, $optcontact, $opturl, $optemail, $optphone, $optnotes, $st);
	}
}

?>
<form action="addnew.php" method="post">
<table width="500" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="white"  bordercolor="white">
<tr>
	<td valign="top">Ad Code</td>
	<td valign="top">
		<input type="text" size="20" name="adcode" value="<?=$adcode?>"> 		
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	Enter an ad code, up to 100 letters and/or digits only. This will appear in your tracking url. For example, if you enter campaign1, your tracking url will be http://<?=$domain?>/<?=$udirectory?>/go.php?c=campaign1</span>
	</td>
</tr>
<tr>
	<td valign="top">Url</td>
	<td valign="top">
	<?
		if($url)
			echo "<input type=text size=30 name=url value=".$url.">";
		else
			echo "<input type=text size=30 name=url value=http://>";
	?>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td colspan=2><span class=small>
	This is where your visitors will be sent after clicking on your tracking url. Please make sure you enter http://, for example, http://www.yourdomain.com is valid, www.yourdomain.com is invalid.</span>
	</td>
</tr>
<tr>
	<td valign="top">Group</td>
	<td valign="top">			
			<select name="group">
			<?
			$groups = mysql_query("select * from adtrackz_groups order by groupname asc");
			if(!$group)
			{
				echo "<option value=\"default\" selected>- default -</option>";
				while($group1 = mysql_fetch_row($groups))
						echo "<option value=\"$group1[0]\">$group1[1]</option>";
			}
			else
			{
				echo "<option value=\"default\">- default -</option>";
				while($group1 = mysql_fetch_row($groups))
				{
					if ($group == $group1[0])
						echo "<option value=\"$group1[0]\" selected>$group1[1]</option>";
					else
						echo "<option value=\"$group1[0]\">$group1[1]</option>";
				}
			}
			?>
			</select>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	Select the group you'd like the campaign to belong to. You can use groups to categorize your campaigns which makes it easier to view their statistics. You can create your own groups in the campaigns menu.
	</span>
	</td>
</tr>
<tr>
	<td valign="top">Purpose</td>
	<td valign="top">			
		<?
			if (!$purpose || $purpose == "clicks")
				$clicks_s = "selected";
			elseif ($purpose == "actions")
				$actions_s = "selected";
			elseif ($purpose == "sales")
				$sales_s = "selected";
			elseif ($purpose == "all")
				$all_s = "selected";
			elseif ($purpose == "affiliate")
				$affiliate_s = "selected";
		?>
		<select name="purpose">
		<option value="clicks" <?=$clicks_s?>>Clicks Only</option>
		<option value="actions" <?=$actions_s?>>Clicks & Actions</option>
		<option value="sales" <?=$sales_s?>>Clicks & Sales</option>
		<option value="all" <?=$all_s?>>Clicks, Actions & Sales</option>
		<option value="affiliate" <?=$affiliate_s?>>Affiliate Link</option>
		</select> 
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	Identify whether the purpose of your campaign is to track clicks, actions or sales, or all three.
	Choose Affiliate Link if you want to track clicks, actions and sales for any affiliate program
	you might be promoting.
	</span>
	</td>
</tr>
<tr>
	<td valign="top">Split Test Stats Only</td>
	<td valign="top">			
		<?
			if ($st)
				$st = "checked";
		?>
		<input type=checkbox name=st <?=$st?>>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	Check this box if this campaign will be used in a split test and you don't want it to show up
	in your main statistics report.
	</span>
	</td>
</tr>
<tr>
	<td valign="top">Cost</td>
	<td valign="top">
		<?if (!$cost)
		{?>
			<input type="text" size="6" maxlength="10" name="cost" value="0.00"> 
		<?}
		else
		{?>
			<input type="text" size="6" maxlength="10" name="cost" value="<?=$cost?>"> 
		<?}?>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td colspan=2><span class=small>
	Enter the cost of your campaign. It must be in the 0.00 format. 40.00 is acceptbale, 40 isn't it.
	</span>
	</td>
</tr> 
<tr>
	<td valign="top">Cost Type</td>
	<td valign="top">
		<?
		if ($ppc)
			$ppc_s = "checked";
		if ($recur)
			$recur_s = "checked";
		?>
		<input type="checkbox" name="ppc" <?=$ppc_s?>> PPC, or <br>
		<input type="checkbox" name="recur" <?=$recur_s?>> Recurring over <input type="text" size="2" maxlength="4"  name="recurdays" value=<?=$recurdays?>> days
		 repeating <input type="text" size="2" maxlength="4" name="repeat" value="<?=$repeat?>"> times.
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	Check PPC if it's a pay per click campaign. Alternatively you can specify a cost that recurs over a period of time. Check nothing if it is a one-time cost.</span>
	</td>
</tr>
<tr>
	<td valign="top">Start Date</td>
	<td valign="top">
	<select name="fmonth">
<?
//set the month
if (!$fmonth)
	$now_month = date("n");
else
	$now_month = $fmonth;
$months = array(1 => 'Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$i=1;
foreach ($months as $month)
{
	if ($now_month == $i)
		echo "<option value=\"$i\" selected>$month</option>";	
	else
		echo "<option value=\"$i\">$month</option>";
	$i++;
}
?>
	</select>
	<select name="fday">
<?
//select this day
if (!$fday)
{
	$now_day = date("j");
	$days_in_month = date ("t");
	for ($i=1;$i<=$days_in_month;$i++)
	{
		if ($now_day == $i)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
else
{
	$now_day = $fday;
	//need to get this month
	$days_in_month = date ("t", strtotime("$fday $months[$fmonth] $fyear"));
	for ($i=1;$i<=$days_in_month;$i++)
	{
		if ($now_day == $i)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
?>
	</select>
	<select name="fyear">
<?
//select this year
if (!$fyear)
	$now_year = date("Y");
else
	$now_year = $fyear;
for ($i=2004;$i<=2014;$i++)
{
	if ($now_year == $i)
		echo "<option value=\"$i\" selected>$i</option>";	
	else
		echo "<option value=\"$i\">$i</option>";
}
?>
	</select>
	</td>
</tr>
<tr>
	<td valign="top">End Date</td>
	<td valign="top">
	<select name="tmonth">
<?
//set the month
if (!$tmonth)
	$now_month = 12;
else
	$now_month = $tmonth;
$months = array(1 => 'Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$i=1;
foreach ($months as $month)
{
	if ($now_month == $i)
		echo "<option value=\"$i\" selected>$month</option>";	
	else
		echo "<option value=\"$i\">$month</option>";
	$i++;
}
?>
	</select>
	<select name="tday">
<?
//select this day
if (!$tday)
{
	for ($i=1;$i<=31;$i++)
	{
		if ($i == 31)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
else
{
	$now_day = $tday;
	$days_in_month = date ("t");
	for ($i=1;$i<=$days_in_month;$i++)
	{
		if ($now_day == $i)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
?>
	</select>
	<select name="tyear">
<?
//select this year
if (!$tyear)
	$now_year = 2014;
else
	$now_year = $tyear;
for ($i=2003;$i<=2014;$i++)
{
	if ($now_year == $i)
		echo "<option value=\"$i\" selected>$i</option>";	
	else
		echo "<option value=\"$i\">$i</option>";
}
?>
	</select>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	When your campaign is going to start and end. If the campaign will go on indefinitely, leave the end date as it 
	is.	</span>
	</td>
</tr> 
<tr>
	<td valign="top">Description</td>
	<td valign="top">
		<textarea rows="3" cols="20" name="description"><?=$description?></textarea>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	Enter a short description of your campaign for your records.
	</span>
	</td>
</tr> 
</table>
<!--
<p><u>Optional Information</u>
<p>
Enter any additional information that you would like. These fields are optional.</p>
<table width="500" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="white"  bordercolor="white">
<tr>
    <td>Contact</td>
    <td>
		<input maxlength="40" size="22" name="optcontact" value="<?=$optcontact?>">
	</td>
    <td>Site URL</td>
    <td>
		<input maxlength="255" size="20" name="opturl" value="<?=$opturl?>">
	</td>
</tr>
<tr>
    <td>Email</td>
    <td>
		<input maxlength="40" size=:22" name="optemail" value="<?=$optemail?>">
	</td>
    <td>Phone</td>
    <td>
		<input maxLength="40" size="22" name="optphone" value="<?=$optphone?>">
	</td>
</tr>
<tr>
    <td valign="top">Ad Copy or Notes</td>
    <td colspan="3">
		<textarea name="optnotes" rows="10" cols="40"><?=$optnotes?></textarea>
	</td>
</tr>
</table>
-->
<br><br>
<center>
<input type="submit" name="submit" value="Add New Campaign"  >
</center></form><br>
<p> Want to create new campaigns without filling out a form? <a href="http://www.adtrackzgold.com/manual" target=_new>Click 
  here</a> to learn how to use dynamic campaigns. <br>

<?

require "footer.php";

?>